/**
 * <p>Title: ServiceZMCM61.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午08:07:05  余虎  Create
 *</pre>
 */
package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplanDetail;

/**采购计划---按需求计划
 * <p>Title: ServiceZMCMD4.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午08:07:05  余虎  Create
 *</pre>
 */
public class ServiceZMCMD4 extends ServiceEPBase {

	

	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD4</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:08:04 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD4</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:08:04 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		inInfo.addBlock("Planresult").set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		String fdPlanId = inInfo.getString("fdPlanId");
		String[] KeyIds = fdPlanId.split(";");
		String KeyId = "";
		for(int i=0; i<KeyIds.length; i++)
		{
			KeyId += "'"+KeyIds[i]+"'";
			if(i!= inInfo.getBlock("result").getRowCount()-1)
			{
				KeyId += ",";
			}
		}
		if(!KeyId.equals(""))
	    {
			StaticTools.setQueryParameters(inInfo, "fdPlanId", KeyId, true);//添加查询条件
	    }
		EiInfo outInfo = super.query(inInfo, "ZMCMTeammaterialplanDetail.queryInterfaceCombine", new ZMCMTeammaterialplanDetail(), false, null, EiConstant.queryBlock, "Planresult", "Planresult");
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));
		return outInfo;
	}
	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD4</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:08:04 余虎
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		String fdStockPlanId = inInfo.getString("fdStockPlanId");
		
		for(int i = 0; i < inInfo.getBlock("Planresult").getRowCount(); i++)
		{
			if(inInfo.getBlock("Planresult").getCellStr(i, "fdMaterialType").toString().equals("无编码物资"))
			{
				String fdkeyId = inInfo.getBlock("Planresult").getCellStr(i, "fdKeyId").toString();
				inInfo.getBlock("Planresult").setCell(i, "fdRequirePlan", fdkeyId);//如果为无 编码物资，则给fdRequirePlan赋值
			}
			
			String KeyID= TablePk.getInstance().getNextValue("T_CM_STOCK_PLAN_DETAIL");
			inInfo.getBlock("Planresult").setCell(i, "fdKeyId", KeyID);//主键
			inInfo.getBlock("Planresult").setCell(i, "fdStockPlanId",fdStockPlanId);//主表ID
			
			String fdPurchaseNum = inInfo.getBlock("Planresult").getCellStr(i, "fdPurchaseNum");
			inInfo.getBlock("Planresult").setCell(i, "fdSum", fdPurchaseNum);
		}
		
		EiInfo outInfo = super.insert(inInfo, "ZMCMStockPlanDetail.insert", "Planresult");
		Map inMap = new HashMap();
		inMap.put("fdStockPlanId", fdStockPlanId);
		dao.update("ZMCMStockPlan.updateForMaterialNum", inMap);
		dao.update("ZMCMStockPlan.updateForPlanSum", inMap);//更新计划总价
		
		return outInfo;
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD4</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:08:04 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		// TODO Auto-generated method stub
		return super.update(inInfo);
	}
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD4</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:08:04 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		// TODO Auto-generated method stub
		return super.delete(inInfo);
	}

}
